package com.hsmpay.mobile.service.trans.impl;

import com.hsmpay.common.pojo.BasePojo;
import com.hsmpay.common.pojo.user.User;
import com.hsmpay.common.service.impl.BaseServiceSupport;
import com.hsmpay.mobile.dao.trans.TransRefuseCardDao;
import com.hsmpay.mobile.pojo.trans.TransRefuseCard;
import com.hsmpay.mobile.service.trans.TransRefuseCardService;
import com.hsmpay.mobile.service.user.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 拒绝卡号交易 服务 接口实现类 transRefuseCardService
 * 对应dao RefuseCardDao
 *
 * @author 颜铃璋
 *         创建时间：2013-11-20
 */
@Service("transRefuseCardService")
public class TransRefuseCardServiceImpl extends BaseServiceSupport<BasePojo, Long> implements TransRefuseCardService<BasePojo, Long> {
    @Resource(name = "transRefuseCardDao")
    TransRefuseCardDao<TransRefuseCard, Long> transRefuseCardDao;
    @Resource(name = "userService")
    UserService<User, Long> userService;

    /**
     * 限制卡号
     *
     * @param accountNumber
     * @return 601129
     * @throws Exception
     */
    public boolean isRefuseCard(String accountNumber) throws Exception {
        //前缀限制
        int prefixCount = transRefuseCardDao.queryPrefixCount(accountNumber);
        if (prefixCount > 0) return true;

        //后缀限制
        int suffixCount = transRefuseCardDao.querySuffixCount(accountNumber);
        if (suffixCount > 0) return true;

//		if(accountNumber.startsWith("6011")) return true;
//		else if(accountNumber.startsWith("64")) return true;
//		else if(accountNumber.startsWith("65")) return true;
        return false;
    }

    /**
     * 根据接口返回 检查银行限制
     * 如果满足限制规则  则将用户 结算状态修改为 暂停结算
     *
     * @param userId
     * @param bankMessage
     * @param orderNum
     * @param orderType   1 消费  65 话费充值 66 固话充值
     * @return
     * @throws Exception
     */
    public boolean checkRefuseClear(Long userId, String bankMessage, String orderNum, Long orderType) throws Exception {
        try {
            if (checkRefuseByBankMessage(bankMessage)) {
                StringBuilder sb = new StringBuilder();
                if (orderType.intValue() == 1)
                    sb.append("[消费] ");
                if (orderType.intValue() == -3l)
                    sb.append("[查询余额] ");
                else if (orderType.intValue() == 65)
                    sb.append("[话费充值] ");
                else if (orderType.intValue() == 66)
                    sb.append("[固话充值] ");
                else if (orderType.intValue() == 67)
                    sb.append("[宽带充值] ");

                sb.append("订单号：").append(orderNum);
                sb.append("  银联提示：").append(bankMessage);
                //修改用户 状态
                userService.updateRefuseClearStatus(userId, sb.toString());
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }


    private boolean checkRefuseByBankMessage(String bankMessage) {
        if (bankMessage.indexOf("挂") > -1
//			||  bankMessage.indexOf("吞") > -1
//			|| bankMessage.indexOf("无效卡") > -1
//			|| bankMessage.indexOf("窃") > -1
//			|| bankMessage.indexOf("作弊") > -1
//			|| bankMessage.indexOf("受限制卡") > -1
//			|| bankMessage.indexOf("没收") > -1
                ) {
            return true;
        }
        return false;
    }

    public static void main(String args[]) throws Exception {
        TransRefuseCardServiceImpl rcs = new TransRefuseCardServiceImpl();

        System.out.println(rcs.isRefuseCard("6512220112722482"));
    }
}
